const passport = require('koa-passport')
const LocalStrategy = require('passport-local')
const User = require('../dbs/models/users')

passport.use(new LocalStrategy(async function(userName,userPwd,done){
  let where = {
    userName
  };
  let result = await UserModel.findOne(where)
  if(result!=null){
    if(result.userPwd===userPwd){
      return done(null,result)
    }else{
      return done(null,false,'密码错误')
    }
  }else{
    return done(null,false,'用户不存在')
  }
}))

passport.serializeUser(function(user,done){
  done(null,user)
})

passport.deserializeUser(function(user,done){
  return done(null,user)
})

module.exports = passport
